package com.ruoyi.business.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.Processcarddetail;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.Equipmentparameter;
import com.ruoyi.business.service.IEquipmentparameterService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备参数Controller
 * 
 * @author DL
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/equipmentparameter")
public class EquipmentparameterController extends BaseController
{
    @Autowired
    private IEquipmentparameterService equipmentparameterService;

    /**
     * 查询设备参数列表
     */
    //@RequiresPermissions("business:equipmentparameter:list")
    @GetMapping("/list")
    public TableDataInfo list(Equipmentparameter equipmentparameter) {
        // 1. 获取原始数据列表
        List<Equipmentparameter> list = equipmentparameterService.selectEquipmentparameterList(equipmentparameter);

        // 2. 转换为目标格式
        Map<String, String> resultMap = list.stream()
                .filter(item -> item.getParameterCode() != null) // 过滤掉parameterCode为null的记录
                .collect(Collectors.toMap(
                        Equipmentparameter::getParameterCode, // key: parameterCode
                        item -> item.getDefaultValue() != null ? item.getDefaultValue().toString() : "", // value: defaultValue或空字符串
                        (existing, replacement) -> existing // 如果有重复key，保留已存在的值
                ));

        // 3. 将Map包装成List并返回
        return getDataTable(Collections.singletonList(resultMap));
    }

    /**
     * 导出设备参数列表
     */
    //@RequiresPermissions("business:equipmentparameter:export")
    @Log(title = "设备参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Equipmentparameter equipmentparameter)
    {
        List<Equipmentparameter> list = equipmentparameterService.selectEquipmentparameterList(equipmentparameter);
        ExcelUtil<Equipmentparameter> util = new ExcelUtil<Equipmentparameter>(Equipmentparameter.class);
        util.exportExcel(response, list, "设备参数数据");
    }

    /**
     * 获取设备参数详细信息
     */
    //@RequiresPermissions("business:equipmentparameter:query")
    @GetMapping(value = "/{EquipmentID}")
    public AjaxResult getInfo(@PathVariable("EquipmentID") String EquipmentID)
    {
        return success(equipmentparameterService.selectEquipmentparameterByEquipmentID(EquipmentID));
    }

    /**
     * 新增设备参数
     */
    //@RequiresPermissions("business:equipmentparameter:add")
    @Log(title = "设备参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Equipmentparameter equipmentparameter)
    {
        return toAjax(equipmentparameterService.insertEquipmentparameter(equipmentparameter));
    }

    /**
     * 修改设备参数
     */
    //@RequiresPermissions("business:equipmentparameter:edit")
    @Log(title = "设备参数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipmentparameter equipmentparameter)
    {
        return toAjax(equipmentparameterService.updateEquipmentparameter(equipmentparameter));
    }

    /**
     * 删除设备参数
     */
    //@RequiresPermissions("business:equipmentparameter:remove")
    @Log(title = "设备参数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{EquipmentIDs}")
    public AjaxResult remove(@PathVariable String[] EquipmentIDs)
    {
        return toAjax(equipmentparameterService.deleteEquipmentparameterByEquipmentIDs(EquipmentIDs));
    }

    /**
     * 查询设备参数列表
     */
    //@RequiresPermissions("business:equipmentparameter:list")
    @GetMapping("/listEquipmentStatus")
    public TableDataInfo listEquipmentStatus(Equipmentparameter equipmentparameter) {
        // 1. 获取原始数据列表
        List<Equipmentparameter> list = equipmentparameterService.selectEquipmentparameterList(equipmentparameter);
        return getDataTable(list);
    }
}
